#!/bin/bash
export LANG=zh_CN.UTF-8

# 应用名称
APP_NAME="boot"

# Java虚拟机启动参数
JAVA_OPTS="-Xms512m -Xmx1024m"

# 获取应用的目录
SCRIPT_DIR=$(dirname "$(readlink -f "$0")")
APP_HOME=$(cd "$SCRIPT_DIR/.."; pwd)

# 配置文件目录
CONF_DIR=$APP_HOME/classes

# lib目录
LIB_DIR=$APP_HOME/lib

# CLASSPATH
CLASSPATH=$CONF_DIR
for jar in "$LIB_DIR"/*.jar; do
    CLASSPATH="$CLASSPATH:$jar"
done

# 获取传入的额外参数
PROFILE_ARGS=""
OTHER_ARGS=""
while [[ $# -gt 0 ]]; do
    case $1 in
        --spring.profiles.active=*|--spring.profiles.active=*)
            PROFILE_ARGS="$1"
            shift
            ;;
        --spring.profiles.active|-spring.profiles.active)
            PROFILE_ARGS="--spring.profiles.active=$2"
            shift 2
            ;;
        *)
            OTHER_ARGS="$OTHER_ARGS $1"
            shift
            ;;
    esac
done

# 确保日志目录存在
mkdir -p logs

# 启动应用
echo "Starting $APP_NAME..."
echo "Classpath: $CLASSPATH"
nohup java $JAVA_OPTS -cp "$CLASSPATH" io.geekidea.boot.SpringBootPlusApplication $PROFILE_ARGS $OTHER_ARGS > logs/boot.log 2>&1 &
echo "$APP_NAME started successfully!"